<template>
  <div class="global-box">
    <div class="title">
      <h1>{{ title }}</h1>
      <a-button type="primary" @click="addCourseServe('add')">新增课程服务</a-button>
    </div>
    <a-card :body-style="{ padding: '24px' }" :bordered="false">
      <div class="form-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="课程目录">
                <a-cascader
                  :field-names="{ label: 'value', value: 'id', children: 'children' }"
                  :options="directoryOptions"
                  :load-data="loadData"
                  placeholder="请选择课程目录"
                  change-on-select
                  v-model="queryParam.directory"
                />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="课程ID">
                <a-input v-model="queryParam.id" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="课程服务名称">
                <a-input v-model="queryParam.name" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons" style="float: right">
                <a-button type="primary" @click="serach">筛选</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-wrapper">
        <a-table :columns="columns" :data-source="data" :pagination="pagination">
          <span slot="action" slot-scope="text, record">
            <template>
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm
                placement="top"
                ok-text="删除"
                cancel-text="取消"
                @confirm="handleDelete(record)"
              >
                <template slot="title">
                  <p>是否确认删除该课程服务？</p>
                  <p style="color: #ccc">一旦删除不可撤销，请谨慎处理</p>
                </template>
                <a>删除</a>
              </a-popconfirm>
            </template>
          </span>
        </a-table>
      </div>
      <a-modal
        :title="serveTitle"
        :visible="serveVisible"
        :confirm-loading="serveLoading"
        @ok="serveHandleOk"
        @cancel="serveHandleCancel"
        okText="提交"
        cancelText="取消"
        width="27%"
      >
        <a-form-model
          ref="ruleForm"
          :model="serveForm"
          :rules="serveFormRules"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <a-form-model-item label="服务名称" prop="name">
            <a-input v-model="serveForm.name" placeholder="请输入服务名称" />
          </a-form-model-item>
          <a-form-model-item label="课程目录" prop="directory">
            <a-cascader
              :field-names="{ label: 'value', value: 'id', children: 'children' }"
              :options="directoryOptionsModel"
              :load-data="loadDataModel"
              placeholder="请选择课程目录"
              change-on-select
              v-model="serveForm.directory"
            />
          </a-form-model-item>
          <a-form-model-item label="服务内容" prop="content">
            <a-select v-model="serveForm.content" placeholder="请选择服务内容">
              <a-select-option
                :value="item.value"
                :key="item.value"
                v-for="item in serveContentList"
                >{{ item.label }}</a-select-option
              >
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="服务单次价格" prop="pirce">
            <a-input v-model="serveForm.pirce" placeholder="请输入服务单次价格" />
          </a-form-model-item>
        </a-form-model>
      </a-modal>
    </a-card>
  </div>
</template>

<script>
import { directory, list, add, viewInfo, edit, deletes } from '@/api/product/serve'
const columns = [
  {
    title: '课程服务ID',
    dataIndex: 'courseServiceId',
    align: 'center'
  },
  {
    title: '服务名称',
    dataIndex: 'courseServiceName',
    align: 'center'
  },
  {
    title: '课程目录',
    dataIndex: 'examName',
    align: 'center'
  },
  {
    title: '服务内容',
    dataIndex: 'serviceContent',
    align: 'center'
  },
  {
    title: '服务单价',
    dataIndex: 'servicePrice',
    align: 'center'
  },
  {
    title: '操作人',
    dataIndex: 'updateUserGroupName',
    align: 'center'
  },
  {
    title: '操作时间',
    dataIndex: 'operatorTime',
    align: 'center'
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center'
  }
]
export default {
  data () {
    return {
      title: '课程服务管理',
      queryParam: {
        directory: [],
        id: '',
        name: ''
      },
      columns,
      data: [],
      selectedRows: [],
      pagination: {
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        showSizeChanger: true,
        showQuickJumper: true,
        onShowSizeChange: (current, pageSize) => {
          this.pagination.defaultCurrent = 1
          this.pagination.defaultPageSize = pageSize
        },
        // 改变每页数量时更新显示
        onChange: (current, size) => {
          this.pagination.defaultCurrent = current
          this.pagination.defaultPageSize = size
        }
      },
      directoryOptions: [],
      directoryOptionsModel: [],
      serveVisible: false,
      serveLoading: false,
      serveTitle: '',
      serveForm: {
        name: '',
        directory: [],
        content: undefined,
        pirce: ''
      },
      serveFormRules: {
        name: [
          { required: true, message: '请输入服务名称', trigger: 'blur' }
        ],
        directory: [
          { required: true, message: '请选择课程目录', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请选择服务内容', trigger: 'change' }
        ],
        pirce: [
          { required: true, message: '请输入服务单次价格', trigger: 'blur' }
        ]
      },
      labelCol: { span: 6 },
      wrapperCol: { span: 15 },
      serveContentList: [{
        label: '常规服务',
        value: 1
      }, {
        label: '一对一服务',
        value: 2
      }, {
        label: '测评问卷',
        value: 3
      }],
      viewInfoData: ''
    }
  },
  computed: {
  },
  mounted () {
    this.getDirectory()
    this.getDirectoryModel()
    this.getData()
  },
  methods: {
    // 获取课程服务列表
    getData () {
      const data = {
        currentPage: this.pagination.defaultCurrent,
        pageSize: this.pagination.defaultPageSize
      }
      if (this.queryParam.name !== '') {
        data.courseServiceName = this.queryParam.name
      }
      if (this.queryParam.id !== '') {
        data.courseServiceId = this.queryParam.id
      }
      if (this.queryParam.directory[0]) {
        data.directoryId = this.queryParam.directory[0]
      }
      if (this.queryParam.directory[1]) {
        data.examId = this.queryParam.directory[1]
      }
      if (this.queryParam.directory[2]) {
        data.subjectId = this.queryParam.directory[2]
      }
      if (this.queryParam.directory[3]) {
        data.fourExamId = this.queryParam.directory[3]
      }
      if (this.queryParam.directory[4]) {
        data.fiveExamId = this.queryParam.directory[4]
      }
      if (this.queryParam.directory[5]) {
        data.sixExamId = this.queryParam.directory[5]
      }
      list(data).then(res => {
        if (res.code === '1') {
          this.pagination.total = res.data.totalSize
          this.data = res.data.rows
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 筛选
    serach () {
      this.getData()
    },
    reset () {
      this.queryParam = {
        directory: [],
        id: '',
        name: ''
      }
      this.getData()
    },
    // 新增课程服务
    addCourseServe () {
      this.serveTitle = '添加服务课程'
      this.serveForm = {
        name: '',
        content: undefined,
        pirce: '',
        directory: []
      }
      this.serveVisible = true
    },
    // 表格编辑
    handleEdit (record) {
      this.serveTitle = '编辑服务课程'
      const data = {
        courseServiceId: record.courseServiceId
      }
      viewInfo(data).then(res => {
        if (res.code === '1') {
          this.viewInfoData = res.data
          this.serveForm = {
            name: res.data.courseServiceName,
            content: res.data.serviceContentType,
            pirce: res.data.servicePrice,
            directory: []
          }
          this.serveForm.directory[0] = res.data.directoryId
          this.serveForm.directory[1] = res.data.examId
          this.serveForm.directory[2] = res.data.subjectId
          this.serveForm.directory[3] = res.data.fourExamId
          this.serveForm.directory[4] = res.data.fiveExamId
          this.serveForm.directory[5] = res.data.sixExamId
          this.serveForm.directory = this.serveForm.directory.filter(item => {
            return item !== 0
          })
          this.serveVisible = true
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 表格删除
    handleDelete (record) {
      const data = {
        courseServiceId: record.courseServiceId
      }
      deletes(data).then(res => {
        if (res.code === '1') {
          this.$message.success(res.msg)
          this.getData()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 弹窗提交
    serveHandleOk () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const data = {
            courseServiceName: this.serveForm.name,
            serviceContentType: this.serveForm.content,
            servicePrice: this.serveForm.pirce
          }
          if (this.viewInfoData !== '' && this.viewInfoData.directoryId) {
            data.courseServiceId = this.viewInfoData.directoryId
          }
          if (this.serveForm.directory[0] && this.serveForm.directory[0] !== '') {
            data.directoryId = this.serveForm.directory[0]
          }
          if (this.serveForm.directory[1] && this.serveForm.directory[1] !== '') {
            data.examId = this.serveForm.directory[1]
          }
          if (this.serveForm.directory[2] && this.serveForm.directory[2] !== '') {
            data.subjectId = this.serveForm.directory[2]
          }
          if (this.serveForm.directory[3] && this.serveForm.directory[3] !== '') {
            data.fourExamId = this.serveForm.directory[3]
          }
          if (this.serveForm.directory[4] && this.serveForm.directory[4] !== '') {
            data.fiveExamId = this.serveForm.directory[4]
          }
          if (this.serveForm.directory[5] && this.serveForm.directory[5] !== '') {
            data.sixExamId = this.serveForm.directory[5]
          }
          if (this.serveTitle === '添加服务课程') {
            add(data).then(res => {
              if (res.code === '1') {
                this.serveVisible = false
                this.$message.success(res.msg)
                this.getData()
              }
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            edit(data).then(res => {
              if (res.code === '1') {
                this.serveVisible = false
                this.$message.success(res.msg)
                this.getData()
              }
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        } else {
          return false
        }
      })
    },
    // 弹窗取消
    serveHandleCancel () {
      this.$refs.ruleForm.resetFields()
      this.serveVisible = false
    },
    // 获取课程目录
    getDirectory () {
      const data = {
        type: 0,
        directoryLevel: 1
      }
      directory(data).then(res => {
        if (res.code === '1') {
          res.data.forEach(item => {
            item.isLeaf = false
          })
          this.directoryOptions = res.data
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 课程目录二级-六级
    loadData (selectedOptions) {
      const targetOption = selectedOptions[selectedOptions.length - 1]
      const data = {
        type: 1,
        parentId: targetOption.id,
        directoryLevel: 2
      }
      targetOption.loading = true
      directory(data).then(res => {
        if (res.code === '1') {
          if (res.data.length > 0) {
            res.data.forEach(item => {
              item.isLeaf = false
            })
            targetOption.loading = false
            targetOption.children = [...res.data]
          } else {
            targetOption.loading = false
            this.$message.error('暂无更多数据')
          }
        } else {
          targetOption.loading = false
          this.$message.error(res.msg)
        }
        this.directoryOptions = [...this.directoryOptions]
      }).catch(err => {
        this.$message.error(err.msg)
        targetOption.loading = false
      })
    },
    // 弹出框获取课程目录
    getDirectoryModel () {
      const data = {
        type: 0,
        directoryLevel: 1
      }
      directory(data).then(res => {
        if (res.code === '1') {
          res.data.forEach(item => {
            item.isLeaf = false
          })
          this.directoryOptionsModel = res.data
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 弹出框课程目录二级-六级
    loadDataModel (selectedOptions) {
      const targetOption = selectedOptions[selectedOptions.length - 1]
      const data = {
        type: 1,
        parentId: targetOption.id,
        directoryLevel: 2
      }
      targetOption.loading = true
      directory(data).then(res => {
        if (res.code === '1') {
          if (res.data.length > 0) {
            res.data.forEach(item => {
              item.isLeaf = false
            })
            targetOption.loading = false
            targetOption.children = [...res.data]
          } else {
            targetOption.loading = false
            this.$message.error('暂无更多数据')
          }
        } else {
          targetOption.loading = false
          this.$message.error(res.msg)
        }
        this.directoryOptionsModel = [...this.directoryOptionsModel]
      }).catch(err => {
        this.$message.error(err.msg)
        targetOption.loading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
.form-status-tip {
  text-align: center;
  color: #999;
}
</style>
